package com.hyy.libray.common;

import android.os.Build;
import android.util.Base64;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName: com.hyy.libray.common
 * @Description:
 * @Author: hyy
 * @Date: 2020/5/11
 * @Time: 10:46 AM
 */
public class BaseUtils {

    public static boolean isCanLoadMore(double total, double pageSize, int curPage) {
        if (curPage >= Math.ceil(total / pageSize)) {
            return false;
        }
        return true;
    }

    public static boolean isCanLoadMore(double total, int curPage) {
        if (curPage >= Math.ceil(total / Constant.PAGE_SIZE)) {
            return false;
        }
        return true;
    }

    public static boolean isEmail(String email) {
        if (null == email || "".equals(email)) return false;
        //Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}"); //简单匹配
        Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配
        Matcher m = p.matcher(email);
        return m.matches();
    }

    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    public static String getDateToString(long time, String dateFormat) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat(dateFormat);
        return sf.format(d);
    }

    /**
     * @param time milliseconds
     * @return
     */
    public static String getDateToString(long time) {
        return getDateToString(time, "yyyy-MM-dd");
    }


    public static String fileToBase64(File path) {
        InputStream is = null;
        byte[] data;
        String result = null;
        try {
            is = new FileInputStream(path);
            //创建一个字符流大小的数组。
            data = new byte[is.available()];
            //写入数组
            is.read(data);
            //用默认的编码格式进行编码
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
                result = Base64.encodeToString(data, Base64.NO_WRAP);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }


    public static String getAmPmDateStr(long mill) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd        ahh:mm");

        Date date = new Date(mill);

        return simpleDateFormat.format(date);
    }

    private static long lastClickTime = 0;

    private static long MIN_DELAY_TIME = 500;
    public static boolean isFastClick() {
        boolean flag = true;

        long currentClickTime = System.currentTimeMillis();
        if ((currentClickTime - lastClickTime) >= MIN_DELAY_TIME) {
            flag = false;
        }
        lastClickTime = currentClickTime;
        return flag;

    }
}
